package com.mdc.mansion.api.web.modules.basedata;


import com.alibaba.fastjson.JSONObject;
import com.mdc.mansion.api.web.common.web.BaseController;
import com.mdc.mansion.common.response.Result;
import com.mdc.mansion.common.response.ResultCode;
import com.mdc.mansion.core.modules.basedata.dto.RestaurantPrinterDTO;
import com.mdc.mansion.core.modules.restaurant.RestaurantExtService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping(value = "${adminPath}/printer")
public class RestaurantPrinterController extends BaseController {
    @Autowired
    private RestaurantExtService restaurantExtService;

    @ModelAttribute("restaurantPrinterDTO")
    public RestaurantPrinterDTO getRestaurantPrinterDTO(@RequestParam(required = false) String data) {
        if(StringUtils.isBlank(data)){return null;}
        RestaurantPrinterDTO restaurantPrinterDTO= JSONObject.parseObject(data,RestaurantPrinterDTO.class);
        return restaurantPrinterDTO;

    }
    @RequestMapping("save")
    public Result save(@ModelAttribute("restaurantPrinterDTO")RestaurantPrinterDTO restaurantPrinterDTO){
        Result result=new Result(ResultCode.COMMON_SUCCESS);
        restaurantExtService.saveRestaurantPrinter(restaurantPrinterDTO);
        return result;
    }

    @RequestMapping("delete")
    public Result delete(@ModelAttribute("restaurantPrinterDTO")RestaurantPrinterDTO restaurantPrinterDTO){
        Result result=new Result(ResultCode.COMMON_SUCCESS);
        restaurantExtService.deleteRestaurantPrinter(restaurantPrinterDTO);
        return result;
    }

    @RequestMapping("list")
    public Result list(){
        Result result=new Result(ResultCode.COMMON_SUCCESS);
        List<RestaurantPrinterDTO> restaurantPrinterDTOs=restaurantExtService.getCurrentUserRestaurantPrinters();
        result.setProperty("printerList",restaurantPrinterDTOs);
        return result;
    }
}
